import comm from "comm";
cc.Class({
    extends: comm,

    properties: {
        myD:null,
        sender:null,
        time:0,
        hp:cc.Sprite,
    },

    onLoad(){
        this.node.getChildByName("clickNode").y = this.node.y>0?-70:70
        this.lv = 1;
    },

    refHp(){
        this.hp.fillRangle = this.myD.hp/this.myD.maxHp;
    },

    setInfo(sender){
        this.myD = this.deepClone(G_wuxingfenghuolu.configJson.fangyuta[""+sender]);
        this.myD.jiange = (1-this.lv*0.1)*this.myD.jiange;
        this.myD.maxHp = this.myD.hp;
        this.sender = sender;
        this.loadres(sender,this.node)
        this.node.getChildByName("lv").getComponent(cc.Label).string = "Lv."+this.lv;
        this.refHp();
    },

    openDetailAction(){
        this.node.getChildByName("clickNode").active = !this.node.getChildByName("clickNode").active;
        if(this.node.getChildByName("clickNode").active==false){
            return
        }
        this.loadres(this.sender,this.node.getChildByName("clickNode").getChildByName("icon"));
        this.node.getChildByName("clickNode").getChildByName("n").getComponent(cc.Label).string = this.myD.name;
        this.node.getChildByName("clickNode").getChildByName("detail").getComponent(cc.Label).string = this.myD.des;

        var xiaohao = 100;
        switch(this.lv){
            case 1:xiaohao = 200;break;
            case 2:xiaohao = 400;break;
            case 3:xiaohao = 800;break;
            case 4:xiaohao = 1600;break;
            case 5:xiaohao = 3200;break;
        }
        var huishou = xiaohao/5;

        this.node.getChildByName("clickNode").getChildByName("up").getChildByName("v").getComponent(cc.Label).string = "升级:"+xiaohao;
        this.node.getChildByName("clickNode").getChildByName("sell").getChildByName("v").getComponent(cc.Label).string = "出售:"+huishou;
    },

    desMe(){
        var xiaohao = 100;
        switch(this.lv){
            case 1:xiaohao = 200;break;
            case 2:xiaohao = 400;break;
            case 3:xiaohao = 800;break;
            case 4:xiaohao = 1600;break;
            case 5:xiaohao = 3200;break;
        }
        var huishou = xiaohao/5;
        G_wuxingfenghuolu.gameObj.coin+=huishou;
        G_wuxingfenghuolu.gameObj.huobiRef();
        this.node.destroy();
    },

    upAction(){
        var xiaohao = 100;
        switch(this.lv){
            case 1:xiaohao = 200;break;
            case 2:xiaohao = 400;break;
            case 3:xiaohao = 800;break;
            case 4:xiaohao = 1600;break;
            case 5:xiaohao = 3200;break;
        }

        if(this.lv>=5){
            this.gameHit("已满级");
            return;
        }

        if(G_wuxingfenghuolu.gameObj.coin<xiaohao){
            this.gameHit("金币不足");
            return;
        }
        G_wuxingfenghuolu.gameObj.coin-=xiaohao;
        G_wuxingfenghuolu.gameObj.huobiRef();
        this.lv+=1;
        this.setInfo(this.sender)
        this.openDetailAction();
    },

    loadres(sender,node){
        var path = "ta/"+sender;
        var self = this;
        cc.loader.loadRes(path, cc.SpriteFrame, function (err, spriteFrame) {
		    if (err) {
		        cc.error(err.message || err);
		        return;
		    }
            node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
		});
    },


    update(dt){
        if(this.myD==null){return}
        this.time+=dt;
        if(this.time>this.myD.jiange){
            this.time = 0;

            switch(this.sender){
                case "jin1":
                    this.bullet_create_jin1()    
                break;
                case "jin2":
                    this.bullet_create_jin2()  
                break;
                case "mu1":
                    this.bullet_create_mu1();
                break;
                case "mu2":
                    this.bullet_create_mu2();
                break;
                case "shui1":
                    this.bullet_create_shui1()    
                break;
                case "shui2":break;
                case "huo1":break;
                case "huo2":break;
                case "tu1":break;
                case "tu2":break;
            }
        }
    },

    //======金系 塔1
    bullet_create_jin1(){
        var enemys = G_wuxingfenghuolu.gameObj.findReEnemy(this.node,this.myD.fanwei);
        if(enemys.length<=0){
            return;
        }
        var bullet_jin1 = cc.instantiate(cc.globalRes["prefab_bullet_jin1"]);
        bullet_jin1.parent = G_wuxingfenghuolu.gameObj.bulletsNode;
        bullet_jin1.position = this.node.position;
        bullet_jin1.getComponent("bullet_jin1").setInfo(enemys[0],this.lv);
    },
      //======金系 塔2
      bullet_create_jin2(){
        var bullet_jin2 = cc.instantiate(cc.globalRes["prefab_bullet_jin2"]);
        bullet_jin2.parent = G_wuxingfenghuolu.gameObj.bulletsNode;
        bullet_jin2.position = this.node.position;
        bullet_jin2.getComponent("bullet_jin2").setInfo(this.lv);
    },

    //======木系 塔1
    bullet_create_mu1(){

    },

    bullet_create_mu2(){
        var enemyTmp = G_wuxingfenghuolu.gameObj.findReEnemy(this.node,this.myD.fanwei);
        var enemys = [];
        for(var i = 0;i<enemyTmp.length;i++){
            var isMuing = enemyTmp[i].getComponent(""+enemyTmp[i].name).getIsMu2ing();
            if(isMuing==false){
                enemys.push(enemyTmp[i])
            }
        }
        var targets = [];
        if(enemys.length>this.lv){
            for(var i = 0;i<this.lv;i++){
                targets.push(enemys[i]);
            }
        }else{
            targets = enemys
        }
        for(var i = 0;i<targets.length;i++){
            var bullet_mu2 = cc.instantiate(cc.globalRes["prefab_bullet_mu2"]);
            bullet_mu2.parent = G_wuxingfenghuolu.gameObj.bulletsNode;
            bullet_mu2.getComponent("bullet_mu2").setInfo(this.node.position,targets[i]);
            targets[i].getComponent(targets[i].name).action_mu2(bullet_mu2);
            bullet_mu2.position = cc.v2(0,0);
        }   
    },

     //======水系 塔1
     bullet_create_shui1(){
        var enemys = G_wuxingfenghuolu.gameObj.findBloodEnemy(this.node,this.myD.fanwei);
        if(enemys.length<=0){
            return;
        }
        var bullet_shui1 = cc.instantiate(cc.globalRes["prefab_bullet_shui1"]);
        bullet_shui1.parent = G_wuxingfenghuolu.gameObj.bulletsNode;
        bullet_shui1.position = this.node.position;
        bullet_shui1.getComponent("bullet_shui1").setInfo(enemys[0],this.lv);
    },


 
  


});
